﻿using System;
using System.Windows.Forms;

namespace Frame
{
    public class DelegateControl
    {
        private delegate void addDelegateEditText(TextBox edit, string text);
        private delegate string GetDelegateCmdText(ComboBox cbx);
        private delegate string GetDelegatDataGridViewValue(DataGridView dataGridView, int row, int cell);
        private delegate void SetDelegatDataGridViewValue(DataGridView dataGridView, int row, int cell, string value);
        private delegate int GetDelegatDataGridViewRowsCount(DataGridView dataGridView);
        /// <summary>
        /// 添加文本
        /// </summary>
        /// <param name="edit">文本对话框</param>
        /// <param name="text">文本</param>
        public static void addEditText(TextBox edit, string text)
        {
            try
            {
                if (edit.InvokeRequired)
                {
                    addDelegateEditText del = new addDelegateEditText(addEditText);
                    edit.Invoke(del, new object[] { edit, text });
                }
                else
                {
                    edit.AppendText(text + "\r\n");
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
            }
        }
        /// <summary>
        /// 获取下拉菜单的值
        /// </summary>
        /// <param name="cbx"></param>
        /// <returns></returns>
        public static string getComboBoxSelect(ComboBox cbx)
        {
            try
            {
                if (cbx.InvokeRequired)
                {
                    GetDelegateCmdText del = new GetDelegateCmdText(getComboBoxSelect);
                    IAsyncResult ia = cbx.BeginInvoke(del, new object[] { cbx });
                    return (string)cbx.EndInvoke(ia);
                }
                else
                {
                    return cbx.Text;
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
                return "";
            }
        }

        public static string GetDataGridViewValue(DataGridView dataGridView, int row, int cell)
        {
            try
            {
                if (dataGridView.InvokeRequired)
                {
                    GetDelegatDataGridViewValue del = new GetDelegatDataGridViewValue(GetDataGridViewValue);
                    IAsyncResult ia = dataGridView.BeginInvoke(del, new object[] { dataGridView, row, cell });
                    return (string)dataGridView.EndInvoke(ia);
                }
                else
                {
                    return dataGridView.Rows[row].Cells[cell].Value.ToString();
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
                return "";
            }
        }
        /// <summary>
        /// 设置表格的值
        /// </summary>
        /// <param name="dataGridView">表格对象</param>
        /// <param name="row">行</param>
        /// <param name="cell">列</param>
        /// <param name="value">值</param>
        public static void SetDataGridViewValue(DataGridView dataGridView, int row, int cell, string value)
        {
            try
            {
                if (dataGridView.InvokeRequired)
                {
                    SetDelegatDataGridViewValue del = new SetDelegatDataGridViewValue(SetDataGridViewValue);
                    dataGridView.Invoke(del, new object[] { dataGridView, row, cell, value });
                }
                else
                {
                    dataGridView.Rows[row].Cells[cell].Value = value;
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
            }
        }   
        /// <summary>
        /// 获取表格的行数
        /// </summary>
        /// <param name="dataGridView">表格对象</param>
        /// <returns>行数</returns>
        public static int GetDataGridViewRowsCount(DataGridView dataGridView)
        {
            try
            {
                if (dataGridView.InvokeRequired)
                {
                    GetDelegatDataGridViewRowsCount del = new GetDelegatDataGridViewRowsCount(GetDataGridViewRowsCount);
                    IAsyncResult ia = dataGridView.BeginInvoke(del, new object[] { dataGridView });
                    return (int)dataGridView.EndInvoke(ia);
                }
                else
                {
                    return dataGridView.Rows.Count;
                }

                //int rowCount = 0;

                //if (dataGridView.InvokeRequired)
                //{
                //    // 使用Invoke方法在UI线程上执行委托
                //    dataGridView.Invoke(new Action(() =>
                //    {
                //        rowCount = dataGridView.Rows.Count;
                //    }));
                //}
                //else
                //{
                //    rowCount = dataGridView.Rows.Count;
                //}

                //return rowCount;
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
                return 0;
            }
        }
        /// <summary>
        /// 自动调整所有列的宽度
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void ResizeColumns(DataGridView dataGridView)
        {
            if (dataGridView.InvokeRequired)
            {
                dataGridView.Invoke(new Action(() =>
                {
                    dataGridView.AutoResizeColumns();
                }));
            }
            else
            {
                dataGridView.AutoResizeColumns();
            }
        }

    }
}
